<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>评论管理 - 我的后台</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="__STATIC__/layui/css/layui.css" media="all" />
	<link rel="stylesheet" href="__CSS__/news.css" media="all" />
</head>
<body class="childrenBody">
	<blockquote class="layui-elem-quote news_search">
		<div class="layui-inline">
			<div class="layui-form">
				<div class="layui-input-inline">
					<input type="text" name="nickname" class="layui-input" id="nickname" placeholder="会员昵称">
				</div>				
				<div class="layui-input-inline">
					<input type="text" name="title" class="layui-input" id="title" placeholder="文章标题">
				</div>
			</div>
		</div>
		<div class="layui-inline">
			<a class="layui-btn" id="search-btn" data-type="reload" href="javascript:;">搜索</a>
		</div>
		<div class="layui-inline">
			<a class="layui-btn layui-btn-danger batchDel">批量删除</a>
		</div>
	</blockquote>
	<div class="layui-form news_list">
		<table class="layui-hide" id="demo" lay-filter="demo"></table>
	</div>
	<script type="text/html" id="id-temp">
		{{#  if(d.read == 1){ }}
			{{d.comment_id}}
		{{#  } else { }}
			<em style="padding: 0px 6px;color: #f50;">#</em>{{d.comment_id}}
		{{#  } }}
	</script>
	<script type="text/html" id="type-temp">
		{{#  if(d.type == 1){ }}
			<span>用户留言</span>
		{{#  } else if(d.type == 2) { }}
			<span>后台回复</span>
		{{#  } }}
	</script>

	<script type="text/html" id="status-temp">
		<input type="checkbox" name="status" value="{{d.comment_id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="statusDemo" {{ d.status == 1 ? 'checked' : '' }}>
	</script>

	<script type="text/html" id="barDemo">
		<a class="layui-btn layui-btn-xs" title="详情" lay-event="detail">详情</a>
		<a class="layui-btn layui-btn-xs layui-btn-normal" title="讨论" lay-event="discuss">讨论({{d.discuss}})</a>
		<a class="layui-btn layui-btn-xs layui-btn-danger" title="删除" lay-event="del">删除</a>
	</script>

	<script type="text/javascript" src="__STATIC__/layui/layui.js"></script>
	<script type="text/javascript">
		
		layui.use(['form', 'layer', 'table'],function(){
			var form  = layui.form,
				layer = layui.layer,
				table = layui.table,
				$ = layui.$;

				layer.msg("加载中...", {'time':1000});

				table.render({
					elem: '#demo'
					,page:true
					,url:'/admin/comment/index?type=1'			
					,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
					,cols: [[
						{type:'checkbox', fixed: 'left'}
						,{field:'comment_id', title: 'ID', width:100, sort: true, templet:"#id-temp"}
						,{field:'nickname', title: '用户名', width:100}
						,{field:'title', title: '文章', width:200}
						,{field:'like_count', title: '点赞', width:200, sort: true}
						,{field:'discuss', title: '讨论', width:150, sort: true}
						,{field:'user_ip', title: 'IP地址', width: 150}
						,{field:'status', title: '状态', width: 150, sort: true, templet:"#status-temp"}
						,{field:'create_time', title: '添加时间', width: 250, sort: true}
						,{fixed:'right', title: '操作',toolbar:"#barDemo", minWidth: 180}
					]]
				});	

		        //搜索
		        var active = {
		            reload: function(){
		                table.reload('demo', {
		                    page: {
		                        curr: 1 //重新从第 1 页开始
		                    },
		                    where: {
		                        nickname: $('#nickname').val(),
		                        title: $('#title').val(),
		                    }
		                });
		            }
		        };

		        $('.news_search #search-btn').on('click', function(){
		            var type = $(this).data('type');
		            active[type] ? active[type].call(this) : '';
		        });

				//监听状态操作
				form.on('switch(statusDemo)', function(obj){
					var status = 1;
					var id = this.value;
					if (!obj.elem.checked) {
						status = -1;
					}
                    $.ajax({
                        url: "/admin/comment/change_status",
                        type: "POST",
                        data: {"id": id, 'status':status},
                        dataType: "json",
                        success: function (data) {
                            if (data.status == '1') {
                                layer.msg(data.msg, {icon: 6});
                            } else {
                                layer.msg(data.msg, {icon: 5});
                            }
                        }
                    });

				});

		        //监听工具条
		        table.on('tool(demo)', function(obj){
		            var data     = obj.data,
		                layEvent = obj.event;
		            if(layEvent === 'detail') {
						var id   = data.comment_id;
		            	layer.open({
		            		type:2,
		            		title:"查看评论",
		            		content: "/admin/comment/detail?comment_id="+id,
		            		area: ['600px', '300px']
		            	})

		            } else if(layEvent === 'discuss'){
		            	var id = data.comment_id;
		            	location.href = "/admin/comment/discuss?comment_id="+id
		            	
		            }else if(layEvent === 'del'){
		                layer.confirm('确定删除？', function (index) {
		                    //向服务端发送删除指令
		                    var comment_id = data.comment_id;
		                    $.ajax({
		                        url: "/admin/comment/delcomment",
		                        type: "POST",
		                        data: {"comment_id": comment_id},
		                        dataType: "json",
		                        success: function (data) {
		                            if (data.status == '1') {
		                                //删除这一行
		                                obj.del();
		                                //关闭弹框
		                                layer.close(index);
		                                layer.msg(data.msg, {icon: 6});
		                            } else {
		                                layer.msg(data.msg, {icon: 5});
		                            }
		                        }
		                    });
		                });
		            }
		        });

		        $('.batchDel').click(function(){
					var checkStatus = table.checkStatus('demo');
					var data = checkStatus.data;
					var arr  = new Array();
					for (var i = 0; i < data.length; i++) {
						arr.push(data[i].comment_id);
					}

	                layer.confirm('确定删除？', function (index) {
	                    //向服务端发送删除指令
	                    $.ajax({
	                        url: "/admin/comment/delcomment",
	                        type: "POST",
	                        data: {"ids": JSON.stringify(arr)},
	                        dataType: "json",
	                        success: function (data) {
	                            if (data.status == '1') {
	                                layer.msg(data.msg, {icon: 6}, function(){
	                            		location.reload();
	                                });
	                            } else {
	                                layer.alert(data.msg, {icon: 5});
	                            }
	                        }
	                    });
	                });
		        })

		})	
	</script>
</body>
</html>